<template>
  <div>
    <div>点画面</div>
    <div ref="webgl" id="webgl" style="width: 800px; height: 500px">1212</div>
  </div>
</template>
<script lang="ts" setup>
  import * as THREE from 'three';
  import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
  import { onMounted, ref } from 'vue';

  const webgl: any = ref(null);
  onMounted(() => {
    init();
  });

  function init() {
    const scene = new THREE.Scene();
    const width = 800;
    const height = 500;
    const camera = new THREE.PerspectiveCamera(30, width / height, 1, 10000);
    camera.position.set(-100, 300, 300);
    camera.lookAt(scene.position);
    const render = new THREE.WebGLRenderer();
    render.setSize(width, height);
    console.log(' webgl.value', webgl.value);

    webgl.value.appendChild(render.domElement);
    // document.body.appendChild(render.domElement);
    new OrbitControls(camera, render.domElement);
    const amLight = new THREE.AmbientLight(0xffffff);
    scene.add(amLight);
    const helper = new THREE.AxesHelper(200);
    scene.add(helper);

    //   几何体
    const geometryBox = new THREE.BoxGeometry(50, 50, 50);
    //   材质
    const materialBox = new THREE.MeshLambertMaterial({
      color: 0xffff00ff,
      transparent: true,
      opacity: 0.4,
    });
    const meshBox = new THREE.Mesh(geometryBox, materialBox);
    //   scene.add(meshBox);

    const geometry = new THREE.BufferGeometry();
    //   坐标点
    var vertices = new Float32Array([
      0,
      0,
      0, //顶点1坐标

      50,
      0,
      0, //顶点2坐标

      50,
      50,
      0, //顶点3坐标

      0,
      50,
      0, //顶点4坐标

      // 0,
      // 0,
      // 100, //顶点5坐标

      // 50,
      // 0,
      // 0, //顶点6坐标
    ]);
    geometry.attributes.position = new THREE.BufferAttribute(vertices, 3);
    //   点复用
    const multiplex = new Uint16Array([0, 1, 2, 2, 3, 0]);
    geometry.index = new THREE.BufferAttribute(multiplex, 1);
    // 坐标颜色
    const colors = new Float32Array([
      1,
      0,
      0, //顶点1颜色
      0,
      1,
      0, //顶点2色
      0,
      0,
      1, //顶点3颜色
      1,
      1,
      0, //顶点4领色
      // 0,
      // 1,
      // 1, //顶点5颜色
      // 1,
      // 6,
      // 1, //顶点6领色
    ]);
    geometry.attributes.color = new THREE.BufferAttribute(colors, 3);

    //   材质
    const material = new THREE.MeshBasicMaterial({
      // color: 0x0000ff,
      vertexColors: false, //以顶点领色为准
      side: THREE.DoubleSide, // 两面可见,
      // size: 5.0, //点对象萦尺
    });

    //   模型
    const mesh = new THREE.Mesh(geometry, material);
    scene.add(mesh);

    // 环境光
    const light = new THREE.AmbientLight(0xffffffff, 0.7);
    scene.add(light);
    // 点光源
    const point = new THREE.PointLight(0xffd41c0f, 0.6);
    point.position.set(200, 200, 200);
    scene.add(point);
    //点光源辅助
    const pointLightHelper = new THREE.PointLightHelper(point, 2);
    scene.add(pointLightHelper);

    function renderFun() {
      // console.log('asdfasdf');
      render.render(scene, camera);
      requestAnimationFrame(renderFun);
    }
    renderFun();
  }
</script>
